package com.service.sys;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.common.base.BaseService;
import com.entity.sys.SysDept;
import com.entity.sys.SysRoleDept;
import com.entity.sys.SysUser;
import com.mapper.sys.SysDeptMapper;
import com.mapper.sys.SysRoleDeptMapper;
import com.mapper.sys.SysUserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 部门 服务实现类
 */
@Service
public class SysDeptService extends BaseService<SysDeptMapper, SysDept>{

    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysRoleDeptMapper sysRoleDeptMapper;

    /**
     * 列表
     */
    public List<SysDept> list(Map<String, String> map){
        sysParams(map,"id",null, true);
        return baseMapper.list(map);
    }

    /**
     * 根据角色id查询部门id列表
     */
    public List<String> selectIdListByRoleId(Map<String, String> map){
        QueryWrapper<SysRoleDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",map.get("id"));
        queryWrapper.select("dept_id");
        List<SysRoleDept> list = sysRoleDeptMapper.selectList(queryWrapper);
        List<String> idList = list.stream().map(SysRoleDept::getDeptId).collect(Collectors.toList());
        return idList;
    }

    /**
     * 根据parentId得到所有子节点的id和name，以逗号分隔
     */
    public Map<String,String> getChildIdAndName(String parentId) {
        List<SysDept> list = childIdList(parentId);
        if (!list.isEmpty()){
            Map<String,String> map = new HashMap<>();
            List<String> idList = list.stream().map(SysDept::getId).collect(Collectors.toList());
            List<String> nameList = list.stream().map(SysDept::getName).collect(Collectors.toList());
            map.put("id",StrUtil.join(",", idList));
            map.put("name",StrUtil.join(",", nameList));
            return map;
        }
        return null;
    }

    /**
     * 递归查询，根据父级id获取子级列表（包括父级本身）
     */
    public List<SysDept> childIdList(String parentId){
        return baseMapper.childList(parentId);
    }

    /**
     * 校验公司/部门名称是否唯一
     */
    public boolean checkDeptName(SysDept dept) {
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysDept::getName,dept.getName());
        queryWrapper.lambda().eq(SysDept::getParentId,dept.getParentId());
        if (StrUtil.isNotBlank(dept.getId())){
            queryWrapper.lambda().ne(SysDept::getId,dept.getId());
        }
        int count = this.baseMapper.selectCount(queryWrapper);
        return count>0 ? true : false;
    }

    /**
     * 公司是否存在子节点或部门
     */
    public boolean hasChildByDeptId(String deptId) {
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",deptId);
        return baseMapper.selectCount(queryWrapper) > 0 ? true : false;
    }

    /**
     * 查询部门是否存在用户
     * @param deptId 部门ID
     * @return 结果 true 存在 false 不存在
     */
    public boolean checkDeptExistUser(String deptId) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept_id",deptId);
        return sysUserMapper.selectCount(queryWrapper) > 0 ? true : false;
    }

    /**
     * 查询部门是否分配了角色
     * @param deptId 部门ID
     * @return 结果 true 存在 false 不存在
     */
    public boolean checkDeptExistRole(String deptId) {
        QueryWrapper<SysRoleDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept_id",deptId);
        return sysRoleDeptMapper.selectCount(queryWrapper) > 0 ? true : false;
    }

    /**
     * 根据名称获取部门
     */
    public String getIdByName(String name){
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        queryWrapper.select("id");
        SysDept dept = baseMapper.selectOne(queryWrapper);
        if (dept != null){
            return dept.getId();
        }
        return null;
    }
}
